<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/8
 * Time: 17:40
 */

namespace app\applet\controller;


use app\share\model\Token;
use think\Db;
use think\Validate;

class Invoice
{

    public function __construct()
    {
        $this->Invoice=new \app\applet\model\Invoice();
        $this->token=new Token();
    }


    /**
     * 添加发票抬头
     */
    public function addTitle(){
        $param=input();
        $validate = new Validate([
            ['invoice_type', 'require', '发票类型不能为空'],
            ['invoice_title', 'require', '发票抬头不能为空'],
            ['credit_code', 'require', '统一社会信用代码不能为空'],
            ['title_company', 'require', '单位名称名称不能为空'],
            ['title_addr', 'require', '抬头地址不能为空'],
            ['title_phone', 'require', '抬头注册电话不能为空'],
            ['title_bank', 'require', '抬头银行不能为空'],
            ['title_account', 'require', '银行账号不能为空'],
            ['title_email', 'require', '接收邮箱不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id = get_header_id("token");

        $this->Invoice->addTitle($member_id,$param);

        returnJson(200,'提交成功');

    }

    /**
     * 删除发票抬头
     */
    public function removeTitle(){
        $param=input();
        $validate = new Validate([
            ['title_id', 'require', '发票抬头ID不能为空'],

        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id = get_header_id("token");
        $param['member_id']=$member_id;

        $this->Invoice->removeTitle($param);

        returnJson(200,'删除成功');

    }
    /**
     * 获取抬头列表
     */
    public function titleList(){
        $param=input();
        $validate = new Validate([
            ['invoice_type', 'require', '发票类型不能为空'],

        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id = get_header_id("token");

        $current_page = empty($input['current_page']) ? 1 : $input['current_page'];
        $page_num = empty($input['page_num']) ? 10: $input['page_num'];
        $titleList=$this->Invoice->titleList($member_id,$param,$current_page,$page_num);
        returnJson(200,'获取成功',$titleList);

    }

    /**
     * 商城消费
     */
    public function shopOrder(){
        $member_id = get_header_id("token");

        $shopOrder=$this->Invoice->shopOrder($member_id);
        returnJson(200,'获取成功', $shopOrder);
    }

    /**
     * 添加发票
     */
    public function addInvoice(){
        $input=input();
        $validate = new Validate([
            ['invoice_source', 'require', '发票来源不能为空'],
            ['invoice_type', 'require', '发票类型不能为空'],
            ['title_type', 'require', '抬头类型不能为空'],
            ['invoice_content', 'require', '发票内容不能为空'],
            ['invoice_email', 'require', '接收邮箱不能为空'],
        ]);
        if(!$validate->check( $input))
        {
            returnJson(-2003,$validate->getError());
        }
        //1=电子普通发票,2=增值税专用发票
        if( $input['invoice_type']==1){
            if($input['title_type']==1){//个人及政府单位
                $validate = new Validate([
                    ['invoice_title', 'require', '发票抬头不能为空'],

                ]);
            }elseif($input['title_type']==2){
                $validate = new Validate([
                    ['credit_code', 'require', '发票类型不能为空'],
                    ['invoice_title', 'require', '发票抬头不能为空'],
                ]);
            }

            if(!$validate->check( $input))
            {
                returnJson(-2003,$validate->getError());
            }
        }elseif( $input['invoice_type']==2){
            $validate = new Validate([
                    ['invoice_title', 'require', '发票抬头不能为空'],
                    ['credit_code', 'require', '发票类型不能为空'],
                    ['title_company', 'require', '单位名称不能为空'],
                    ['title_addr', 'require', '单位地址不能为空'],
                    ['title_phone', 'require', '注册电话不能为空'],
                    ['title_bank', 'require', '银行名称不能为空'],
                    ['title_account', 'require', '银行账号不能为空'],
            ]);
            if(!$validate->check( $input))
            {
                returnJson(-2003,$validate->getError());
            }
        }
        $member_id = get_header_id("token");

        $data['update_time']=time();

        $this->Invoice->addInvoicer($member_id,$input);
        returnJson(200,'操作成功');
    }

    /**
     * 发票列表
     */
    public function invoiceList(){
        $input=input();
        //$validate = new Validate([
          //  ['invoice_source', 'require', '发票来源不能为空'],
       // ]);
       // if(!$validate->check( $input))
       // {
         //   returnJson(-2003,$validate->getError());
       // }
        $member_id = get_header_id("token");

        $current_page = empty($input['current_page']) ? 1 : $input['current_page'];
        $page_num = empty($input['page_num']) ? 10: $input['page_num'];
        $where['member_id']=$member_id;
        $where['invoice_source']=$input['invoice_source'];
        $where['is_delete']=1;
        $titleList=$this->Invoice->invoiceList($where,$current_page,$page_num);

        returnJson(200,'获取成功',$titleList);
    }
    /**
     * 获取抬头信息
     */
    public function getTitleInfo(){
        $input=input();

            $validate = new Validate([
                ['title_id', 'require', '抬头ID不能为空'],

            ]);
        if(!$validate->check( $input))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id = get_header_id("token");
        $input['member_id']=$member_id;
        $titleList=$this->Invoice->invoiceTitleInfo($input);
        returnJson(200,'获取成功',$titleList);
    }


}